<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
    protected $bizUser = array();
    protected $datas   = array();
    protected $bizId   = 0;

    public function __construct() {
        parent::__construct();
        date_default_timezone_set('Asia/Shanghai');
    }

    private function checkSign() {
        $params = array_merge($_GET, $_POST);
        if (!isset($params['sign']) || !isset($params['timestamp'])) {
            return false;
        }
        $sign = $params['sign'];
        unset($params['sign']);
        $t = time();
        $timeStamp = $params['timestamp'];
        $sub = $t - $timeStamp;
        if ($sub > 5 || $sub < -5) {
            return false;
        }

        ksort($params);
        $paramStr = array();
        foreach ($params as $key => $val) {
            $paramStr[] = $key.'='.urldecode($val);
        }
        $paramStr = implode('&', $paramStr);

        $signCal = hash('sha256', $paramStr);
        if ($signCal != $sign) {
            return false;
        }

        return true;
    }

    protected function checkLogin() {
        if (isset($_SESSION['bizUser']) && $_SESSION['bizUser'] && is_array($_SESSION['bizUser']) && isset($_SESSION['bizUser']['username']) && $_SESSION['bizUser']['username'] && isset($_SESSION['bizUser']['userId']) && $_SESSION['bizUser']['userId'] && is_numeric($_SESSION['bizUser']['userId']) && $_SESSION['bizUser']['userId'] > 0) {
            $this->bizUser = $_SESSION['bizUser'];
        }

        if (!$this->bizUser) {
            return false;
        }

        $this->load->model('Bizuser_model');
        $bizUserInfo = $this->Bizuser_model->getBizUserById($this->bizUser['userId']);
        if (!$bizUserInfo) {
            $this->bizUser = array();
            return false;
        }

        $this->bizUser['nickname'] = $bizUserInfo['nickname'];
        $this->bizUser['username'] = $bizUserInfo['username'];
        $this->bizUser['appId']    = $bizUserInfo['appId'];
        $this->bizUser['secret']   = $bizUserInfo['secret'];
        $this->bizId = $bizUserInfo['id'];

        return true;
    }
}
